// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

const db = uniCloud.database();
let canNext;
module.exports = {
	/**
	 * method1方法描述
	 * @param {object} param1 参数1描述
	 * @returns {string} 返回值描述
	 */
	async doZhuCe(obj, creattime) {
		// 参数校验，如无参数则不需要
		if (!obj) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		canNext = await db.collection("roomcenteruser").where({
			name: obj.uname
		}).get();
		if (canNext.affectedDocs) {
			return {
				errcode: 5210
			} //用户名已存在
		}

		if (obj.componentName != "wx") {
			// 判断手机号是否已经存在
			canNext = await db.collection('roomcenteruser').where({
				phone: obj.phoneNum
			}).get();
			if (canNext.affectedDocs) {
				return {
					errcode: 5008
				} //手机号已存在
			}
		}
		//找回（修改密码功能）
		if (obj.componentName == 'fb') {
			let res = await db.collection('roomcenteruser').where({
				phone: obj.phoneNum
			}).update({
				pwd: obj.pwd
			});
			return res;
		}

		//手机注册
		if (obj.componentName == 'pl') {
			let res = await db.collection('roomcenteruser').add({
				phone: obj.phoneNum,
				ishave: 0,
				avatar: "",
				use: 1,
				creattime: creattime
			});
			return res;
		}

		//微信注册
		if (obj.componentName == 'wx') {
			let res = await db.collection('roomcenteruser').add({
				name: obj.uname || "",
				pwd: obj.pwd || "",
				openid: obj.openid,
				avatar: "",
				ishave: 0,
				use: 1,
				creattime: creattime
			});
			return res;
		}

		//注册功能
		let res = await db.collection('roomcenteruser').add({
			name: obj.uname || "",
			pwd: obj.pwd || "",
			phone: obj.phoneNum,
			avatar: "",
			ishave: 0,
			use: 1,
			creattime: creattime

		});
		return res;
	}
}